package cn.wolfcode.service.impl;

import cn.wolfcode.domain.StrategyCatalog;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.repsitory.StrategyCatalogRepsitory;
import cn.wolfcode.service.IStrategyCatalogService;
import cn.wolfcode.vo.CatalogVO;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Description: StrategyCatalog业务逻辑实现类
 * @Author: 伍家钰
 * @Date: 2020/8/7 16:20
 * @Version: 1.0
 */
@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {
    @Resource
    private StrategyCatalogRepsitory strategyCatalogRepsitory;
    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalog.setId(null);
        strategyCatalogRepsitory.save(strategyCatalog);
    }

    @Override
    public void delete(String id) {
        strategyCatalogRepsitory.deleteById(id);
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
        strategyCatalogRepsitory.save(strategyCatalog);
    }

    @Override
    public StrategyCatalog get(String id) {

        return strategyCatalogRepsitory.findById(id).orElse(null);
    }

    @Override
    public List<StrategyCatalog> list() {
        return null;
    }


    @Override
    public Page<StrategyCatalog> query(QueryObject qo) {
        Query query = new Query();
        // 总数据
        long total = mongoTemplate.count(query, StrategyCatalog.class);
        if (total == 0) {
            // 如果返回null会报错
            return Page.empty();
        }

        // 当前页显示的数据,Pageable是PageRequest的父接口的父接口
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        query.with(pageable);
        List<StrategyCatalog> list = mongoTemplate.find(query, StrategyCatalog.class);
        return new PageImpl<>(list, pageable, total);
    }

    @Override
    public List<CatalogVO> groupList() {
        List<CatalogVO> list = new ArrayList<>();
        //聚合查询
        TypedAggregation<StrategyCatalog> agg = Aggregation.newAggregation(StrategyCatalog.class,
                Aggregation.group("destName").
                        push("name").as("names").
                        push("id").as("ids")
        );
        AggregationResults<Map> result = mongoTemplate.aggregate(agg, Map.class);
        List<Map> datas = result.getMappedResults(); //将结果转化成list<map>   map描述一行数据
        for (Map data : datas) {
            CatalogVO vo = new CatalogVO();

            vo.setDestName(data.get("_id").toString());  //id
            List<Object> names = (List<Object>) data.get("names");  //攻略分类的名称集合
            List<Object> ids = (List<Object>) data.get("ids");  //攻略分类的ids集合

            List<StrategyCatalog> catalogList = new ArrayList<>(); //攻略分类对象

            for (int i = 0; i < names.size(); i++) {
                StrategyCatalog sc = new StrategyCatalog();
                String name = names.get(i).toString();
                String id = ids.get(i).toString();
                sc.setId(id);
                sc.setName(name);
                catalogList.add(sc);
            }
            vo.setCatalogList(catalogList);
            list.add(vo);
        }
        return list;
    }

    @Override
    public List<StrategyCatalog> findByDestId(String id) {
        return strategyCatalogRepsitory.findByDestId(id);
    }

}
